<?php
/** 
 * 
 *
 * @package api
 * @subpackage lang
 * @author Richard Fullmer <php-api@byu.edu>
 * @version 1.0
 */
/**
 *
 */
require_once( "lang/Number.php" );
require_once( "lang/IllegalArgumentException.php" );
require_once( "lang/Comparable.php" );

/**
 * Class Boolean
 *
 * The Boolean class wraps a value of the primitive type boolean in an object. An object of type Boolean contains a single field 
 * whose type is boolean.
 * 
 * In addition, this class provides many methods for converting a boolean to a String and a String to a boolean, as well as other 
 * constants and methods useful when dealing with a boolean. 
 *
 * @package api
 * @subpackage lang
 */
class Boolean extends Object implements Comparable {

	// ======================================= Private Members ======================================================
	
	/**
	 * Internally stores the boolean value represented by this object.
	 */
	private $value;
	
	// ======================================= Public Methods =======================================================
	
	/**
	 * Allocates a Boolean object representing the $value argument.
	 *
	 * @param boolean $value - the value of the Boolean
	 */
	public function __construct( $value ) {
		if ( is_bool( $value ) )
			$this->value = $value;
		else
			throw new IllegalArgumentException();
	}
	
	/**
	 * Returns true if and only if the argument is not null and is a Boolean object that represents the same boolean value as this object.
	 *
	 * @param Object $obj the object to compare with.
	 * @returns boolean true if the Boolean object represents the same value, false otherwise.
	 */
	public function equals( Object $obj ) {
		if ($o instanceof Boolean) {
			return ( $this->booleanValue() == $o->booleanValue() );
		}
		return false;
	}
	
	/**
	 * Compares this Boolean instance with another.
	 *
	 * @param Object $o the object instance to be compared.
	 * @returns int zero if this object represents the same boolean value as the argument; a positive value if this object represents 
	 * 		true and the argument represents false; and a negative value if this object represents false and the argument represents true.
	 * @throws NullPointerException - if the argument is null
	 * @throws ClassCastException - if the object being compared is not a Boolean object.
	 */
	public function compareTo( Object $o ) {
		if (!$o instanceof Boolean )
			throw new ClassCastException();
		if (is_null($o))
			throw new NullPointerException();
			
		if ($this->booleanValue() == $o->booleanValue())
			return 0;
		else if ($this->booleanValue() == true and $o->booleanValue() == false)
			return 1;
		else
			return -1;
	}
	
	/**
	 * Returns the value of this Boolean as a boolean primitive.
	 *
	 * @returns boolean the primitive boolean value of this object.
	 */
	public function booleanValue() {
		return $this->value;
	}
	
	/**
	 * Returns a hash code for this Boolean object.
	 *
	 * @returns int the integer 1231 if this object represents true; returns the integer 1237 if this object represents false.
	 */
	public function hashCode() {
		return $this->value ? 1231 : 1237;
	}
	
	/**
	 * Returns a String object representing this Boolean's value.  If this object represents the value true, a string equal to "true" is
	 * returned.  Otherwise, a string equal to "false" is returned.
	 *
	 * @returns string a string representation of this object.
	 */
	public function toString() {
		return $this->value ? "true" : "false";
	}
}

?>